package com.mingqijia.gassafety.webserver.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.mingqijia.gassafety.db.entity.OperateRecord;
import com.mingqijia.gassafety.db.entity.OperationLog;
import com.mingqijia.gassafety.db.entity.vo.OperateTypeVO;
import com.mingqijia.gassafety.db.mapper.OperateRecordMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.constant.OperateRecordEnum;
import com.mingqijia.gassafety.shared.constant.PageColumnConst;
import com.mingqijia.gassafety.shared.utils.ApplicationUtils;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageColumns;
import com.mingqijia.gassafety.shared.utils.commonUtils.PageUtil;
import com.mingqijia.gassafety.webserver.request.OperateRecordRequest;
import com.mingqijia.gassafety.webserver.response.OperateRecordResponse;
import com.mingqijia.gassafety.webserver.service.OperateRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.mingqijia.gassafety.shared.constant.Constants.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author li.shiying
 * @since 2023-09-22
 */
@Service
@Slf4j
public class OperateRecordServiceImpl extends ServiceImpl<OperateRecordMapper, OperateRecord> implements OperateRecordService {

    @Override
    public PageUtil<OperateRecordResponse> getList(OperateRecordRequest request) {

        String spId = ApplicationUtils.getWorkingSpId();
        LambdaQueryWrapper<OperateRecord> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OperateRecord::getMenu, request.getMenuCode()).eq(OperateRecord::getSpId, spId).eq(OperateRecord::getIsDeleted, Constants.IS_DELETED_FALSE);
        if (StringUtil.isNotEmpty(request.getOperator())){
            lqw.and(
                    wrapper -> wrapper.like(OperateRecord::getOperatorCode,request.getOperator())
                            .or().like(OperateRecord::getOperatorName,request.getOperator()));
        }
        lqw.between(request.getStartDate() != null, OperateRecord::getCreatedAt, request.getStartDate(), request.getEndDate())
                .eq(request.getOperateType() != null , OperateRecord::getOperateType, request.getOperateType())
                .like(request.getCommonInfo() != null, OperateRecord::getCommonInfo, request.getCommonInfo())
                .eq(request.getResult() != null, OperateRecord::getOperateResult, request.getResult())
                .eq(request.getConsumerId() != null, OperateRecord::getConsumerId, request.getConsumerId())
                .eq(StringUtils.isNotEmpty(request.getStrategyType()), OperateRecord::getStrategyType, request.getStrategyType())
                .orderByDesc(OperateRecord::getCreatedAt);
        Page<OperateRecord> page = PageHelper.startPage(request.getPage(), request.getPageSize());
        List<OperateRecord> list = this.list(lqw);
        List<OperateRecordResponse> collect = list.stream().map(item -> {
            OperateRecordResponse response = new OperateRecordResponse();
            BeanUtils.copyProperties(item, response);
            response.setOperateResultCode(item.getOperateResult());
            if (OPERATE_RESULT_SUCCESS.equals(item.getOperateResult())){
                response.setOperateResult(OPERATE_RESULT_SUCCESS_MSG);
            }else if (OPERATE_RESULT_FAIL.equals(item.getOperateResult())){
                response.setOperateResult(OPERATE_RESULT_FAIL_MSG);
            }else if (OPERATE_RESULT_SUCCESS_PART.equals(item.getOperateResult())){
                response.setOperateResult(OPERATE_RESULT_SUCCESS_PART_MSG);
            }
            response.setOperator(item.getOperatorName() + "(" + item.getOperatorCode() + ")");
            response.setOperateTypeName(OperateRecordEnum.getInfo(item.getOperateType()).getOperationType());
            return response;
        }).collect(Collectors.toList());
        PageUtil<OperateRecordResponse> pageInfo = new PageUtil<>(request.getPage(), request.getPageSize(), page.getTotal(),collect);
        return pageInfo;
    }

    @Override
    public List<OperateTypeVO> getType(OperateRecordRequest request) {
        List<OperateTypeVO> responseList = new ArrayList<>();
        List<OperateRecordEnum> list = OperateRecordEnum.getList(request.getMenuCode());
        if (CollectionUtils.isEmpty(list)){
            return null;
        }
        for (OperateRecordEnum operateRecordEnum : list) {
            OperateTypeVO vo = new OperateTypeVO();
            vo.setOperateTypeCode(operateRecordEnum.getCode());
            vo.setOperateTypeName(operateRecordEnum.getOperationType());
            vo.setMenu(operateRecordEnum.getMenu());
            responseList.add(vo);
        }
        return responseList;

    }
}
